<?php

namespace App\Console\Commands;

use App\Http\Models\UserAmountLog;
use App\Http\Models\ZtkAccess;
use App\Http\Models\ZtkOrder;
use App\Http\Models\ZtkTask;
use App\Http\Models\ZtkUser;
use App\Jobs\AutoOrder;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;

class TaskStatus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'TaskStatus {type}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '定时修改发布的任务状态，未完成罚款';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $type = $this->argument('type');

        //删除监控表记录
        $time = Carbon::now()->subDays(2);
        ZtkAccess::where('created_at','<',$time)->forceDelete();

        //0点 结束今日单和次日单
        if($type == 1){
            $data = ZtkTask::whereIn('send_date',[1,2,4])->whereIn('status',[1,3])->get();
            foreach($data as $k=>$v)
            {
                DB::beginTransaction();
                try {
                    $task = ZtkTask::find($v->id);
                    $task->status = 5;
                    $task->save();

                    //返回金额
                    if($task->type == 2){
                        $returnMoney = ZtkOrder::where('task_id',$v->id)->whereIn('status',[1,3,4])->sum('offer_price');
                    }else{
                        $returnMoney = ($task->total_num - $task->finish_num) * $task->price;
                    }

                    ZtkOrder::where('task_id',$v->id)->where('status',1)->update(['status'=>3]);

                    if($returnMoney > 0){
                        $user = ZtkUser::find($task->user_id);
                        $user->amount = $user->amount + $returnMoney;
                        $user->save();

                        //任务退回金额
                        UserAmountLog::create([
                            'user_id'  => $user->id,
                            'trade_no' => get_order_no($user->id),
                            'amount'   => $returnMoney,
                            'after_amount' => $user->amount,
                            'type'     => 1,
                            'class'    => 7,
                        ]);
                    }

                    DB::commit();
                } catch (QueryException $e) {
                    DB::rollBack();
                }
            }
        }

        //8:30点 开始次日单
        if($type == 2){
            $Start = date("Y-m-d 00:00:00",strtotime("-1 day"));
            $End = date('Y-m-d 23:59:59', strtotime("-1 day"));

            $list = ZtkTask::where('send_date',2)->where('status',2)->whereBetween('created_at',[$Start,$End])->get();
            ZtkTask::where('send_date',2)->where('status',2)->whereBetween('created_at',[$Start,$End])->update(['status'=>1]);

            if($list->isNotEmpty()){
                foreach($list as $key=>$value){
                    dispatch(new AutoOrder($value->id));
                }
            }
            unset($list);
        }

        //17:30点 开始过夜单
        if($type == 3){
            $Start = date('Y-m-d 00:00:00', time());
            $End = date('Y-m-d 23:59:59', time());

            $list = ZtkTask::where('send_date',3)->where('status',2)->whereBetween('created_at',[$Start,$End])->get();
            ZtkTask::where('send_date',3)->where('status',2)->whereBetween('created_at',[$Start,$End])->update(['status'=>1]);

            if($list->isNotEmpty()){
                foreach($list as $key=>$value){
                    dispatch(new AutoOrder($value->id));
                }
            }
            unset($list);
        }

        //23:00点 结束过夜单
        if($type == 4){
            $data = ZtkTask::where('send_date',3)->whereIn('status',[1,3])->get();
            foreach($data as $k=>$v)
            {
                DB::beginTransaction();
                try {
                    $task = ZtkTask::find($v->id);
                    $task->status = 5;
                    $task->save();

                    //返回金额
                    if($task->type == 2){
                        $returnMoney = ZtkOrder::where('task_id',$v->id)->whereIn('status',[1,4])->sum('offer_price');
                    }else{
                        $returnMoney = ($task->total_num - $task->finish_num) * $task->price;
                    }

                    ZtkOrder::where('task_id',$v->id)->where('status',1)->update(['status'=>3]);

                    if($returnMoney > 0){
                        $user = ZtkUser::find($task->user_id);
                        $user->amount = $user->amount + $returnMoney;
                        $user->save();

                        //任务退回金额
                        UserAmountLog::create([
                            'user_id'  => $user->id,
                            'trade_no' => get_order_no($user->id),
                            'amount'   => $returnMoney,
                            'after_amount' => $user->amount,
                            'type'     => 1,
                            'class'    => 7,
                        ]);
                    }

                    DB::commit();
                } catch (QueryException $e) {
                    DB::rollBack();
                }
            }
        }

        //1:00点 罚款未完成的
        if($type == 5){
            $Start = date("Y-m-d 00:00:00",strtotime("-1 day"));
            $End = date('Y-m-d 23:59:59', strtotime("-1 day"));

            //send_date=4 加急单即时罚款了
            $data = ZtkTask::where('status',5)->where('send_date','<>',4)->whereBetween('updated_at',[$Start,$End])->get();
            foreach($data as $key=>$value)
            {
                $order = ZtkOrder::where('task_id',$value->id)->whereIn('status',[1,3])->get();
                foreach($order as $k=>$v)
                {
                    DB::beginTransaction();
                    try {
                        //罚款金额
                        //$returnMoney = $v->type == 1 ? $v->price * 10 : $v->offer_price * 10;
                        $returnMoney = 28;

                        //罚款
                        $user1 = ZtkUser::find($v->user_id);  //如果钱不够，有多少罚多少

                        //$money = ($user1->amount > $returnMoney) ? $returnMoney : $user1->amount; //实际罚款金额
                        $amount = $user1->amount - $returnMoney; //账户余额 keweifu

                        //if($money > 0){
                            $trade_no = get_order_no($user1->id);

                            $user1->amount = $amount;
                            $user1->save();
                            //罚款记录
                            UserAmountLog::create([
                                'user_id'  => $user1->id,
                                'trade_no' => $trade_no,
                                'amount'   => $returnMoney,
                                'after_amount' => $user1->amount,
                                'type'     => 2,
                                'class'    => 9,
                                'task_id'  => $v->task_id,
                            ]);

                            /*//给发布人
                            $user2 = ZtkUser::find($v->publish_user_id);
                            $user2->amount = $user2->amount + $money;
                            $user2->save();
                            //给发布人记录
                            UserAmountLog::create([
                                'user_id'  => $user2->id,
                                'trade_no' => $trade_no,
                                'amount'   => $money,
                                'after_amount' => $user2->amount,
                                'type'     => 1,
                                'class'    => 10,
                                'task_id'  => $v->task_id,
                            ]);*/
                        //}

                        DB::commit();
                    } catch (QueryException $e) {
                        DB::rollBack(); continue;
                    }
                }
            }
        }
        // --

    }

}
